package com.partners.component.aop;

import com.partners.configure.wechatmp.WxMpConfiguration;
import com.partners.configure.wechatmp.WxMpProperties;
import com.partners.constant.ErrorMessageEnum;
import com.partners.model.BusinessException;
import com.partners.utils.ApiResultUtil;
import com.partners.utils.SessionManager;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.WebUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

import static com.partners.constant.ErrorMessageEnum.HTTP_BAD_REQUEST;


/**
 * @author liangyongle
 * @date 2018/8/21 0021
 */
@Aspect
@Component
@Slf4j
@Order(1)
public class AjaxAuthAop {

    public final static String appId = "wx1b38cf3729aaa2a7";


    @Pointcut("@annotation(com.partners.annotation.AjaxAuths)")
    public void authVaildService(){}

   @Resource
   private WxMpProperties wxMpProperties;

   @Resource
   private SessionManager sessionManager;


    @Autowired
    private HttpServletRequest request;

    @Around("authVaildService()")
    public Object doAuthVaildService(ProceedingJoinPoint pjp) throws Throwable{
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String openId = (String) WebUtils.getSessionAttribute(request, "openId");
        if (openId != null) {
            log.info(openId);
            //缓存openid信息
            return pjp.proceed();
        }
        return ApiResultUtil.failure(ErrorMessageEnum.LOGIN_EXPIRE);
    }

  /*  @After("tokenVaildService()")
    public void doAfter() throws Throwable{
        System.out.println("验证之后");
    }*/
}
